unit main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.Win.ScktComp,
  mClientSession, frmClientManager, mcCommands;

type
  TForm1 = class(TForm)
    masterSocket: TServerSocket;
    btn1: TButton;
    btn2: TButton;
    fileSocket: TServerSocket;
    btn3: TButton;
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure masterSocketClientConnect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure masterSocketClientDisconnect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure masterSocketClientRead(Sender: TObject; Socket: TCustomWinSocket);
    procedure masterSocketClientError(Sender: TObject; Socket: TCustomWinSocket;
      ErrorEvent: TErrorEvent; var ErrorCode: Integer);

    procedure interpretPackage(com: RCommand);
    procedure connect(cs: TClientSession);


    procedure FormCreate(Sender: TObject);
    procedure fileSocketClientRead(Sender: TObject; Socket: TCustomWinSocket);
    procedure fileSocketClientDisconnect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure btn3Click(Sender: TObject);
  private
    { Private declarations }
    filereceivesize, sizereceived: DWORD;

    //all integrated functions


    //method enabling receiving files
    procedure filereceivecomplete;
    procedure enableFileReceive;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  clientList: TList;
  status: DWORD;
  lastchecksum: dword;

const
  STATUS_FILE_RECEIVE = $101;
  STATUS_WAITING = $DEAD;

implementation

{$R *.dfm}

procedure TForm1.connect(cs: TClientSession);
var
  com: RCommand;
begin
//  frmManager.connect;
//  com.magic := MAGIC_DWORD;
//  com.command := COM_CONNECT;
//  com.data := DATA_EMPTY;
//  com.checksum := Random(CHECKSUM_RAND);
//  cs.clientSocket.SendBuf(com, SizeOf(com));
end;

procedure TForm1.filereceivecomplete;
begin
  //comlete
end;

procedure TForm1.enableFileReceive;
begin
  fileSocket.Active := true;
end;

procedure TForm1.fileSocketClientDisconnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  socket.Close;
end;

procedure TForm1.fileSocketClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  //receive file
end;

procedure TForm1.interpretPackage(com: RCommand);
var
  ack: RAck;
begin
  //do it
  
end;

procedure TForm1.btn1Click(Sender: TObject);
begin

  btn1.Enabled := False;
  btn2.Enabled := True;
  masterSocket.Active := True;
end;

procedure TForm1.btn2Click(Sender: TObject);
begin
  btn1.Enabled := True;
  btn2.Enabled := False;
  masterSocket.Active := False;

end;

procedure TForm1.btn3Click(Sender: TObject);
begin
  frmManager.Visible := true;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Randomize;
  clientList := TList.Create;
end;

procedure TForm1.masterSocketClientConnect(Sender: TObject;
  Socket: TCustomWinSocket);
var
  cs: TClientSession;
  com: RCommand;
begin
//we're connected to someone, yay
  cs := TClientSession.Create;
  cs.iLastCommand := 0;
  cs.bIsActiveOne := False;

  //save handle to clientsession in socket's data
  Socket.Data := cs;

  //save socket in clientsession so we can get from one to the other
  cs.clientSocket := Socket;

  clientList.Add(cs);
  //no need to send confirmation as theres an event on the client's side


  com.magic := MAGIC_DWORD;
  com.command := COM_PING;
  com.data := DATA_EMPTY;
  lastchecksum := Random(CHECKSUM_RAND);
  com.checksum := lastchecksum;

  socket.SendBuf(com, SizeOf(com));
  ShowMessage('bufsent');
end;

procedure TForm1.masterSocketClientDisconnect(Sender: TObject;
  Socket: TCustomWinSocket);
var
  cs: TClientSession;
begin
// ;(
  cs := TClientSession(socket.Data);

  clientList.Remove(cs);
  cs.Free;
end;

procedure TForm1.masterSocketClientError(Sender: TObject;
  Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
// ;((
  socket.Close;
  ErrorCode := 0;

  ShowMessage('lost connection :(');

end;

procedure TForm1.masterSocketClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
  cs: TClientSession;
  lpBuffer: Pointer;
  //strBuffer: string;
  dwLen, dwRecv: DWORD;
  com: RCommand;
  ack: RAck;
begin
//reading something
  {dwLen := socket.ReceiveLength;

  if dwLen > 0 then
  begin
    GetMem(lpBuffer, dwLen + 1);

    ZeroMemory(lpBuffer, dwLen + 1);

    //cs := TClientSession(socket.Data);

    dwRecv := socket.ReceiveBuf(lpBuffer^, dwLen);


    if dwRecv > 0 then
    begin
      //interpretPackage(lpBuffer);
    end;

    FreeMemory(lpBuffer);

  end;  }

//  cs := TClientSession(socket.Data);
//
//  if cs.bIsActiveOne then
//  begin
//    //we're interested in this!
//    case status of
//
//    STATUS_WAITING:
//      begin
//        frmManager.setcs(TClientSession(socket.Data));
//        Socket.ReceiveBuf(com, SizeOf(com));
//        frmManager.interpretPackage(com);
//      end;
//
//
//    end;
//
//  end;

  if socket.ReceiveLength = sizeof(com) then
  begin
    socket.ReceiveBuf(com, SizeOf(com));
    if com.command = COM_PONG then
    begin
      ack.magic := MAGIC_DWORD;
      ack.ack := com.checksum;
      socket.SendBuf(ack, SizeOf(ack));
      showmessage('asdlhf');
    end;
  end;



end;


end.
